package com.example.wanxian.booktest;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends AppCompatActivity {
    private List<Notes> mlist=new ArrayList<>();
    private EditText find_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this,"93e73f607ada101555edf51df460abfe");
        initAddNote();
        initRecyclerView();
    }

    private void initRecyclerView() {
        BmobQuery<notebook> notes = new BmobQuery<notebook>();
        notes.findObjects(new FindListener<notebook>() {
            @Override
            public void done(List<notebook> list, BmobException e) {
                if(e==null){
                    for(notebook note : list){
                        Log.e("MainActivity",note.getTitle()+";"+note.getUpdatedAt().substring(0,10));
                        String title = note.getTitle();
                        if(title.length()>10){
                            title = title.substring(0,10)+"...";
                        }
                        String data = note.getContent();
                        if (data.length()>8) {
                            data = data.substring(0,6)+"...";
                        }
                        mlist.add(new Notes(title,data,note.getUpdatedAt().substring(0,10),note.getObjectId()));
                    }
                }else{
                    Log.e("MainActivity","更新内容失败");
                }
            }
        });
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        NoteBookBaseAdapter adapter = new NoteBookBaseAdapter(this,mlist);
        recyclerView.setAdapter(adapter);
    }

    private void initAddNote() {
        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.addNote);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });
    }
}
